package com.gzw.shop.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.gzw.shop.bean.Wares;

import java.util.Iterator;
import java.util.List;

/**
 * Created by JACKYGUO on 2017-03-09.
 * <p>
 * Adapter的封装
 */

public abstract class BaseAdapter<T, H extends BaseViewHolder> extends RecyclerView.Adapter<BaseViewHolder> {

    protected List<T> mDatas;

    protected LayoutInflater mInflater;

    protected Context mContext;

    protected int mLayoutId;

    private OnItemClickListener mListener;

    //设置点击事件
    public void setListener(OnItemClickListener listener) {
        mListener = listener;
    }

    public interface OnItemClickListener {
        void OnClick(View view, int position);
    }

    public BaseAdapter(Context context, List<T> datas, int layoutId) {
        this.mDatas = datas;
        this.mContext = context;
        this.mLayoutId = layoutId;
        mInflater = LayoutInflater.from(mContext);
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = mInflater.inflate(mLayoutId, parent, false);
        return new BaseViewHolder(view, mListener);
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {

        T t = getItem(position);
        bindData(holder, t);
    }

    public T getData(int position) {
        return mDatas.get(position);
    }


    //获取所有数据
    public List<T> getDatas() {
        return mDatas;
    }

    //清除数据
    public void clearData() {
        mDatas.clear();
        notifyItemRangeRemoved(0, mDatas.size());
    }

    //添加数据
    public void addData(List<T> datas) {
        addData(0, datas);
    }

    public void addData(int position, List<T> datas) {

        if (datas != null && datas.size() > 0) {
            mDatas.addAll(datas);
            notifyItemRangeChanged(position, mDatas.size());
        }
    }


    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    public T getItem(int position) {
        return mDatas.get(position);
    }

    //绑定数据
    public abstract void bindData(BaseViewHolder holder, T t);

    //清除数据
    public void clear() {

        if (mDatas == null || mDatas.size() <= 0)
            return;

        for (Iterator it = mDatas.iterator(); it.hasNext(); ) {
            T t = (T) it.next();
            int position = mDatas.indexOf(t);
            it.remove();
            notifyItemRemoved(position);
        }
    }

    //刷新数据
    public void refreshData(List<T> list) {
        if (list != null && list.size() > 0) {
            clear();
            int size = list.size();
            for (int i = 0; i < size; i++) {
                mDatas.add(i, list.get(i));
                notifyItemInserted(i);
            }
        }
    }

    //加载更多数据
    public void loadMoreData(List<T> list) {
        if (list != null && list.size() > 0) {

            int size = list.size();
            int begin = mDatas.size();
            for (int i = 0; i < size; i++) {
                mDatas.add(list.get(i));
                notifyItemInserted(i + begin);
            }
        }
    }

}
